package handlers

import (
	"github.com/gin-gonic/gin"
	"pet-manage/models"
)

func Add(c *gin.Context) {
	name := c.PostForm("name")
	types := c.PostForm("types")
	if name == "" || types == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Pet{
		Name:  name,
		Types: types,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}
func List(c *gin.Context) {
	var pet []models.Pet
	models.DB.Order("id desc").Find(&pet)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "列表展示成功",
		"data": pet,
	})
	return

}
